package FlashServer;

import java.io.IOException;
import java.util.LinkedList;

public class MessageProcessor 
{
	private int FBattleRating;
	private int FBattleTrack;
	private String FBattleUserName;
	
	private SocketHandler FHandler;	
	private SocketHandler FEnemy;
	private LinkedList<SocketHandler> FRooms;
	
	public MessageProcessor(SocketHandler AHandler)
	{		
		FHandler = AHandler;
		FEnemy = null;
		FBattleRating = -1;
		FBattleTrack = -1;
		FBattleUserName = new String();
	}
	
	public void Init(LinkedList<SocketHandler> ARooms)
	{
		FRooms = ARooms;
	}

	public void Process(DataMessage AMessage) throws IOException
	{		
		switch (AMessage.DataType)
		{
			case MessageID.NewBattle:
			{
				DoOnNewBattle(AMessage);
				FHandler.SendToAll(AMessage);
				break;
			}			
			case MessageID.RemoveBattle:
			{
				DoOnRemoveBattle();
				FHandler.SendToAll(AMessage);
				break;
			}			
			case MessageID.BattlesListRequest:
			{
				DoOnListRequest();				
				break;
			}
			
			case MessageID.AcceptBattle:
			{
				DoOnBattleAccept(AMessage);
				break;
			}
			case MessageID.Beat:
			{
				if(FEnemy!=null)
					FEnemy.Send(AMessage);
				break;
			}
		}
	}
	
	private void DoOnBattleAccept(DataMessage AMessage) throws IOException
	{
		FEnemy = FHandler.getHandlerByName(AMessage.Message);
		if(FEnemy != null)
		{			
			setEnemy(FEnemy);
			FEnemy.getMessageProcessor().setEnemy(FHandler);
			FHandler.Send(AMessage);
						
			AMessage.Message = FHandler.getUserName();
			FEnemy.Send(AMessage);
		}
	}
	
	private void DoOnRemoveBattle()
	{
		FBattleUserName = "";
		FBattleRating = -1;
		FBattleTrack = -1;
		FRooms.remove(FHandler);
	}

	private void DoOnNewBattle(DataMessage AMessage)
	{		
		String[] vBattleParams = AMessage.Message.split(",");
		FBattleUserName = vBattleParams[1];
		FBattleRating = AMessage.Points;
		FBattleTrack = AMessage.Index;
		FRooms.add(FHandler);
	}
	
	public void DoOnListRequest() throws IOException
	{		
		synchronized (FRooms) 
		{
			String vResult = new String();			
			for (SocketHandler AHandler : FRooms)			
				vResult += AHandler.getMessageProcessor().BattleParams();
			
			System.out.println("Battles data: " + vResult);
			
			if(!vResult.equals(""))
			{
				DataMessage vMessage = new DataMessage();				
				vMessage.Message = vResult;
				vMessage.DataType = MessageID.BattlesList;
				FHandler.Send(vMessage);
			}
		}
	}	
	
	public void DoOnClose() throws IOException
	{
		DoOnRemoveBattle();
		DataMessage vUserExit = new DataMessage();
		vUserExit.DataType = MessageID.ClientExit;
		vUserExit.Message = FHandler.getUserName();
		FHandler.Send(vUserExit);				
	}
	
	public String BattleParams()
	{
		if(FBattleTrack > -1)
		{
			return FHandler.getUserName() + "," + FBattleUserName + "," + FBattleRating + "," + FBattleTrack + ";";
		}
		else
			return "";
	}
	
	public void setEnemy(SocketHandler AEnemy)
	{
		if(AEnemy != null)
		{
			DoOnRemoveBattle();
			FEnemy = AEnemy;			
		}
	}
}